package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/user")
public class UserController {


    @Reference
    private UserService userService;

    /*有条件就条件查询，无条件就查询全部*/
    @GetMapping("/page")
    public Result userPage(QueryPageBean queryPageBean) {
        return userService.userPage(queryPageBean);
    }

    /*新建一个用户,给用户绑定权限*/
    @PostMapping
    public Result roleIds(@RequestParam("roleIds") String idStr, @RequestBody User user) {
         userService.user(idStr,user);
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }
    /*编辑显示消息（user）*/
    @GetMapping("/{id}")
    public Result selectUser(@PathVariable("id") Integer id){
       return userService.selectUser(id);
    }


//    /*编辑显示消息（user）*/
//    @GetMapping("/getUsername")
//    public Result getUsername(@PathVariable("id") Integer id) {
//        return null;
//    }



    /*编辑显示消息（user）*/
    @GetMapping("/{id}/roleIds")
    public Result selectUserRole(@PathVariable("id") Integer id){

        return userService.selectUserRole(id);
    }

    /*编辑添加方法*/
    @PutMapping
    public Result selectUser1(@RequestParam("roleIds") String idStr, @RequestBody User user){
        System.out.println(idStr);
        userService.user1(idStr,user);
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    @DeleteMapping("/{id}")
    public Result deletByUserId(@PathVariable("id")Integer id){
        userService.deletByUserId(id);
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }



//    获取当前用户名称
    @GetMapping("/getUsername")
    public Result getUsername(){
        //使用Spring Security 的获取用户名方法获取名称
        String name = "admin";
        return new Result(true,"菜单显示成功",name);
    }

//    通过用户名获取菜单列表
    @GetMapping("/menuLists")
    public Result menuLists(@RequestParam String username){

        List<Menu> list = userService.findMuneByUserName(username);
        if (list.size()>0){
            return new Result(true,"查询菜单成功",null);
        }else{
            return new Result(false,"查询菜单失败",null);
        }

    }
}
